<? include 'server_config.php'; 
		
$article_per_page=15;

$sql="
	SELECT A.id, A.content, B.nickname, A.up_count, A.down_count, A.reg_timestamp, A.user_id
	FROM nddg_talk A, ndd_user B
	WHERE 
	A.user_id = B.user_idx
	ORDER BY A.reg_timestamp DESC LIMIT ".($page)*$article_per_page.";
";

$result = mysql_query($sql,$connect);

while($row = mysql_fetch_row($result))
{
			
?>
	<div style='width:700px; float:left;'>
		<b><?=$row[2]?></b> - <?=$row[5]?><br>
		<br>
		<?=$row[1]?><br>
		<br>

		<? 
			$sub_sql="
				SELECT A.id, A.content, B.nickname, A.up_count, A.down_count, A.reg_timestamp, A.user_id
				FROM nddg_talk_comment A, ndd_user B
				WHERE 
				A.talk_id = '".$row[0]."' AND
				A.user_id = B.user_idx
				
				ORDER BY A.reg_timestamp ;
			";

			$sub_result = mysql_query($sub_sql,$connect);

			while($sub_row = mysql_fetch_row($sub_result))
			{
				?>
				<div style='width:600px; float:left; padding-left:100px;'>
				<b><?=$sub_row[2]?></b> - <?=$sub_row[5]?><br>
				<br>
				<?=$sub_row[1]?><br>
				<br>
				</div>
				<?
			}

		if($user_idx!="")
		{
			
			?>
			<div style='width:600px; float:left; padding-left:100px;'>
				<form id='fwrite_<?=$row[0]?>'>
					<textarea name='new_talk_<?=$row[0]?>' id="new_talk_<?=$row[0]?>" style='width:500px;height:50px;'></textarea>
					<div style='float:right;width:80px;height:20px;padding-top:30px;'>
						<input type='button' value='댓글달기' onclick="talk_register_comment('<?=$row[0]?>');">
					</div>
					<input type='hidden' name='talk_id' value='<?=$row[0]?>'>
				</form>			
			</div>
			<div style='width:700px; float:left; height:10px;'></div>
			<?
		}
		?>

		<hr>
	</div>
	<br>
	<br>
<?
}
?>

<? 

$sql="
	SELECT count(*)
	FROM nddg_talk ;
";

$result = mysql_query($sql,$connect);
$row = mysql_fetch_row($result);

$article_count = $row[0];

	if ($article_count>(($page)*$article_per_page) )
	{
?> 
			<div style='height:30px;width:100%;float:left;'><span></span></div>
			<div style='float:left;height:50px;width:100%;text-align:center;'><span>
			<img id="img_show_more" src="./img/talk_view_more.png" alt="더보기" style="cursor:pointer;border:0px;" onclick="talk_display_list('<? echo $page+1;?>' ); " />
			</span>
			</div>
			<div style='height:30px;width:100%;float:left;'><span></span></div>
		
	<?
	}
	else
	{
	?>
			<div style='height:30px;width:100%;float:left;'><span></span></div>
			
			<div style='height:60px;width:100%;float:left;text-align:center;'>
			마지막 페이지 입니다.</div>
	<?
	}
	?>

<br><br>


<script>

	function talk_register_comment(talk_id)
	{
		var string = $("#fwrite_"+talk_id).serialize(); 
				
		$.ajax(
		{
			type: "POST",
			url: "nddg_talk_comment_new.php",
			data: string,
			
			success: function(msg) {
				if(msg==1)
				{
					talk_display_list(<?=$page?>);
					$('#new_talk_'+talk_id).val("");
					
				}
				else
				{
					alert(msg);
				}
			}
			
		})		

	}
</script>		